/**
 * 
 */
package com.cqan.account.rest.impl;

import static com.cqan.util.http.HttpStatus.*;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.core.HttpHeaders;

import org.springframework.stereotype.Component;

import com.cqan.account.entity.Favorite;
import com.cqan.account.entity.resp.FavoritePageResp;
import com.cqan.account.entity.resp.FavoriteResp;
import com.cqan.account.rest.FavoriteRestService;
import com.cqan.account.service.FavoriteService;
import com.cqan.common.ResultResp;
import com.cqan.digital.entity.Digital;
import com.cqan.digital.service.DigitalService;

import static com.cqan.common.Constant.*;

import com.cqan.orm.Page;
import com.cqan.orm.PropertyFilter;
import com.cqan.service.rest.BaseRestService;
import com.google.common.collect.Lists;

/**
 * @Description 用户收藏rest接口实现
 * @author slaton.wu@gmail.com
 * @date 2012-7-19下午2:21:07
 * 
 */
@Component("favoriteRestService")
public class FavoriteRestServiceImpl extends BaseRestService  implements FavoriteRestService {

	@Resource(name = "favoriteService")
	private FavoriteService favoriteService;

	@Resource(name = "digitalService")
	private DigitalService digitalService;

	@Override
	public FavoriteResp getFavorite(Long id, HttpHeaders headers) {
		FavoriteResp resp = new FavoriteResp();
		try {
			Favorite favorite = favoriteService.get(id);
			if (favorite == null) {
				resp.setCode(NO_CONTENT);
				return resp;
			}
			resp.setFavorite(favorite);
			resp.setCode(OK);
		} catch (Exception e) {
			resp.setCode(INTERNAL_SERVER_ERROR);
			e.printStackTrace();
		}
		return resp;
	}

	@Override
	public ResultResp addFavorite(Long nid, HttpHeaders headers) {
		ResultResp resp = new ResultResp();
		try {
			Map<String, String> header = getHeader(headers);
			String ab = header.get("ab");
			String uid = header.get("uid");
			Digital digital = digitalService.get(nid);
			if (uid == null || "".equals(uid) || ab == null || "".equals(ab)
					|| digital == null) {
				resp.setCode(BAD_REQUEST);
				return resp;
			}
			Favorite favorite = new Favorite();
			favorite.setCreateTime(Calendar.getInstance().getTime());
			favorite.setFavoriteType("NODE");
			favorite.setName(digital.getTitle());
			favorite.setNid(nid);
			favorite.setPid(Long.parseLong(ab));
			favorite.setUid(Long.parseLong(uid));
			favorite.setOrig("SITE");
			resp.setCode(OK);
			favoriteService.add(favorite);
		} catch (Exception e) {
			resp.setCode(INTERNAL_SERVER_ERROR);
			e.printStackTrace();
		}
		return resp;
	}

	@Override
	public ResultResp deleteFavorite(Long id, HttpHeaders headers) {
		ResultResp resp = new ResultResp();
		try {
			Map<String, String> header = getHeader(headers);
			String ab = header.get("ab");
			String uid = header.get("uid");
			if (uid == null || "".equals(uid) || ab == null || "".equals(ab)) {
				resp.setCode(BAD_REQUEST);
				return resp;
			}
			favoriteService.delete(id);
			resp.setCode(OK);
		} catch (Exception e) {
			resp.setCode(INTERNAL_SERVER_ERROR);
			e.printStackTrace();
		}
		return resp;
	}

	@Override
	public FavoritePageResp favoriteList(HttpHeaders headers) {
		FavoritePageResp resp = null;
		try {
			Map<String, String> header = getHeader(headers);
			String ab = header.get("ab");
			String uid = header.get("uid");
			if (uid == null || "".equals(uid) || ab == null || "".equals(ab)) {
				resp = new FavoritePageResp();
				resp.setCode(BAD_REQUEST);
				return resp;
			}
			Page<Favorite> page = new Page<Favorite>(50);
			List<PropertyFilter> filters = Lists.newArrayList();
			filters.add(new PropertyFilter("EQL_pid", ab));
			filters.add(new PropertyFilter("EQL_uid", uid));
			page = favoriteService.search(page, filters);
			resp = new FavoritePageResp(page);
			if (page!=null&&page.getResult()!=null&&page.getResult().size()>0) {
				resp.setCode(OK);
			}else{
				resp.setCode(NO_CONTENT);
			}
		} catch (Exception e) {
			resp.setCode(INTERNAL_SERVER_ERROR);
			e.printStackTrace();
		}
		return resp;
	}

}
